package com.xp.ai.chat.openai;

import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.CustomMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.request.ChatRequestParameters;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.openai.OpenAiChatModel;
import org.apache.catalina.User;

import java.util.HashMap;

/**
 * openai 简单聊天模型
 */
public class OpenAiSimpleChat {
    public static void main(String[] args) {
        ChatLanguageModel model = OpenAiChatModel.builder()
                .apiKey("demo")
                .defaultRequestParameters(ChatRequestParameters.builder()
                        .modelName("gpt-4o-mini")
                        .temperature(0.7)
                        .build())
                .build();
        // 基础对话
        /*UserMessage userMessage1 = UserMessage.userMessage("你是谁？");
        ChatResponse chat = model.chat();
        String text = chat.aiMessage().text();
        System.out.println("text = " + text);*/

        /**
         * 消息被抽象为了几类
         * AiMessage  大模型的回复信息
         * CustomMessage 自定义消息，用来表示任何类型的自定义聊天消息。可以是 ai system user 或者是 ToolExecutionResultMessage
         * SystemMessage  系统的提示消息 对用户的消息进行一个封装
         * ToolExecutionResultMessage  本地的工具方法消息
         * UserMessage 用户消息
         */
        //手工实现连续对话
        CustomMessage customMessage = CustomMessage.customMessage(new HashMap<>(10));
        UserMessage userMessage1 = UserMessage.userMessage("你是谁？");
        AiMessage aiMessage = model.chat(userMessage1).aiMessage();
        String text = aiMessage.text();
        System.out.println(text);

        //第二次对话
        UserMessage userMessage2 = UserMessage.from("请重复一遍");
        ChatResponse chatResponse = model.chat(userMessage1, aiMessage, userMessage2);
        AiMessage aiMessage2 = chatResponse.aiMessage();
        String text2 = aiMessage2.text();
        System.out.println(text2);

        //第三次对话
        UserMessage userMessage3 = UserMessage.from("请再重复一遍");
        //0.3的版本是
        ChatResponse response = model.chat(userMessage1,aiMessage, userMessage2, aiMessage2,  userMessage3);
        AiMessage aiMessage3 = response.aiMessage();
        String text3 = aiMessage3.text();
        System.out.println(text3);

    }
}
